package com.sskj.bibi.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.bibi.bean.EntrustBean;
import com.sskj.bibi.http.HttpConfig;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.bibi.ui.activity.AllEntrustActivity;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;


import java.util.List;

import io.reactivex.Flowable;


public class AllEntrustActivityPresenter extends BasePresenter<AllEntrustActivity> {


    public Flowable<List<EntrustBean>> getEntrustList(int page) {
//       return Flowable.rangeLong(0,3)
//                .map(aLong -> new EntrustBean(1,"BTC/USDT","1","1"
//                        ,2,3,23,"34","34","123","123"))
//                .toList()
//                .toFlowable();
        return httpService.getBibiTradeRecord(null,null,"3",page)
                .converter(new JsonConverter<HttpData<PageBean<EntrustBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(pageBeanHttpData -> pageBeanHttpData.getData().getList());

    }

    public void getEntrustListRefresh() {
//        Flowable.rangeLong(0,3)
//                .map(aLong -> new EntrustBean(1,"BTC/USDT","1","1"
//                        ,2,3,23,"34","34","123","123"))
//                .toList()
//                .subscribe(entrustBeans -> {
//                    mView.updateUI(entrustBeans);
//                });
        httpService.getBibiTradeRecord(null,null,"3",1)
                .execute(new JsonCallBack<HttpData<PageBean<EntrustBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<PageBean<EntrustBean>>> response) {
                        HttpData<PageBean<EntrustBean>> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateUI(httpData.getData().getList());
                        }
                    }
                });

    }

    public void cancelEntrust(String orderId) {
        httpService.cancelCoinOrder( orderId)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                        mView.cancelReturn();
                    }
                });
    }
}
